/********************************************************************************************************************************
 * 《C ++ Primer》第五版中文版，读书笔记：按照书中的的示例源码和每一个小章节的课后习题，自己动手写代码实现，作为联系和测试。
 * Date: 2024-03-24 18:05
 * Author: dimon.chen 
 * EMali: 1181302388@qq.com 
 * 
 * 章节: 第二章 变量和基本类型
 *       2.3 复合类型
 *       2.3.1 引用
 * 
 ********************************************************************************************************************************/


///
/// 2.3.1 练习
///

/*
 * 练习2.15 下面哪个定义是不合法的为什么？ 
 *  (a) int iVal = 10;          //合法
 *  (b) int &rval1 = 1.0.1      //不合法，引用类型的初始值必须是一个对象
 *  (c) int &rval2 = ival;      //合法，rval2是一个引用，其与 ival 对象绑定在一起
 *  (d) int &rval3;             //不合法，引用必须被初始化
 * 
 * 练习2.16 考查下面的的所有赋值然后回答：哪些赋值是不合法的？为什么？哪些赋值是合法的？它们执行了什么样的操作？
 *  int i = 0, &r1 = i;         //合法, i是 int 型， r1是一个引用，其与 i 绑定在一起
 *  double d = 2; &r2 = d;      //d合法
 *  (a) r2 = 3.1415926；        //合法，引用定义之后，对其的操作都是在与之绑定的对象上进行的。对引用赋值，其结果是对其绑定的对象赋值。执行结果 d = 3.14.15926
 *  (b) r2 = r1;                //合法，引用定义之后，对其的操作都是在与之绑定的对象是进行的。对引用赋值，其结果是对其绑定的对象赋值。
 *                              //获取引用的值，其结果是获取与之绑定的对象的值。执行结果和 d = i; 把int型赋值给double型，在整型值后面加 .0，结果为 d = 0.0
 *  (c) i = r2;                 //合法，引用定义之后，对其的操作都是在与之绑定的对象上进行的。执行效果为 i = d; 此时d=0.0，把double型赋值给int型变量将舍弃小数
 *                              // i = 0
 *  (d) r1 = d;                 //合法，引用定义之后，对其的操作都是在与之绑定的对象上进行的。对应用赋值，其结果是对其绑定的对象赋值。
 *                              //执行结果 i = d; 把double型赋值给int型，将舍弃小数 i =0
 * 
 * 练习2.17 执行下面的代码将输出怎样的输出结果？
 *  int i, &ri = i;             //i为int，ri是一个引用，其与int型对象i绑定在一起
 *  i = 5; ri = 10;             //i=5，把字面值整型常量赋值给i. ri是一个引用，其与i绑定在一起。引用定义之后，对其的操作都是在与之绑定的对象上进行的。对引用赋值
 *                              //其结果是对与之绑定的对象赋值,执行结果 i = 10;
 *  std::cout << i << "" << ri << std::endl; 
 *                              //ri是一个引用，其与i绑定在一起，是对象i的一个别名。引用定义之后，获取引用的值，其结果是获取与其绑定的对象的值。
 *                              //执行结果为 std::cout << i << " " << i << std::endl;
*/

#include <iostream>

int main(int argc, char *argv[])
{
    ///
    /// 练习2.15
    ///
#if 0
    int iVal = 10;
    int &rVal = 1.01;
    int &rVal2 = iVal;
    int &iVvl3;
#endif

    ///
    /// 练习2.16
    ///

#if 0
    int i = 0, &r1 = i;
    double d = 0, &r2 = d;

    std::cout << "(a)" << std::endl; 
    std::cout << "before " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "before " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "before " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "before " << "r2 is a reference: r2 = " << r2 << std::endl;
    r2 = 3.1415926;
    //d = 3.1415926;
    std::cout << "after " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "after " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "after " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "after " << "r2 is a reference: r2 = " << r2 << std::endl;
   

    std::cout << "(b)" << std::endl; 
    std::cout << "before " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "before " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "before " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "before " << "r2 is a reference: r2 = " << r2 << std::endl;
    r2 = r1; 
    //d = i;  = 0.0
    std::cout << "after " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "after " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "after " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "after " << "r2 is a reference: r2 = " << r2 << std::endl;


    std::cout << "(c)" << std::endl; 
    std::cout << "before " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "before " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "before " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "before " << "r2 is a reference: r2 = " << r2 << std::endl;
    i = r2;
    //i=d
    std::cout << "after " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "after " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "after " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "after " << "r2 is a reference: r2 = " << r2 << std::endl;


    std::cout << "(d)" << std::endl; 
    std::cout << "before " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "before " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "before " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "before " << "r2 is a reference: r2 = " << r2 << std::endl;
    r1 = d;
    //i = d;
    std::cout << "after " <<  "i is base type double: i = " << i << std::endl;
    std::cout << "after " << "r1 is a reference: r1 = " << r1 << std::endl;
    std::cout << "after " <<  "d is base type double: d = " << d << std::endl;
    std::cout << "after " << "r2 is a reference: r2 = " << r2 << std::endl;
#endif

    ///
    /// 练习2.17
    ///

    int i, &ri = i;
    std::cout << "i " << i << " ri " << ri<< std::endl;
    i = 5;
    std::cout << "i " << i << " ri " << ri<< std::endl;
    ri = 10;
    std::cout << "i " << i << " ri " << ri<< std::endl;

}